if (!ORYX.Plugins) ORYX.Plugins = new Object();
ORYX.Plugins.kpilayout = Clazz.extend(
{
    facade: undefined,
    styleNode: undefined,
    construct: function (facade)
    {
        this.facade = facade;
        this.facade.registerOnEvent(ORYX.CONFIG.EVENT_ORYX_SHOWN, this.init.bind(this));
        this.facade.registerOnEvent('layout.pmanage.kpi', this.handleKpiLayout.bind(this));
        this.facade.registerOnEvent(ORYX.CONFIG.EVENT_SEARCHANDHIGHLIGHT_KPILIST, this.saveList.bind(this));

        this.kpilist
    },
    init: function init()
    {

        this.facade.registerOnEvent(ORYX.CONFIG.EVENT_PROPERTY_CHANGED, this.shapeNameUnique.bind(this));
    },
    saveList: function saveList(event){

          this.kpilist = [];
           this.kpilist.push( event.kpilist);
          console.log("this kpilist: %o", this.kpilist);

    },

    handleKpiLayout: function (event)
    {
        var shape = event.shape;     //get the parent shape of the layout event

        function rHex()
        {
            return '#000000'.replace(/0/g, function ()
            {
                return (~~ (Math.random() * 16)).toString(16);
            });
        }
        var labels = shape.getChildNodes(false).findAll(function (node)
        {
            return (node.getStencil().id() === "http://b3mn.org/stencilset/pmanage#kpi");  // get the underlying kpis
        });

        var offset = 0;           //define offset for kpi positioning

        if (labels.length > 0)
        {
            labels.each(function (label)
            {
                var ul = label.bounds.upperLeft();
                var lr = label.bounds.lowerRight();
                ul.y = 3;
                ul.x = 3 + offset;
                lr.y = 3 + label.bounds.height();
                lr.x = 3 + label.bounds.width() + offset;
                label.bounds.set(ul, lr);
                if (label.properties["oryx-color"] === "#ffffff") label.setProperty("oryx-color", rHex());   // if the kpi has no specific color yet, give it a random color
                offset += label.bounds.width() + 6;
            });
        }
    },
    shapeNameUnique: function shapeNameUnique(event)
    {
     /*   console.log("shu event: %o", event);
        if (event.elements[0]._stencil._jsonStencil.title === "Key Performance Indicator"){

            for(var i = 0; i < this.kpilist.length ; i++){

               if(event.elements[0].properties["oryx-name"] === this.kpilist[i].properties["oryx-name"]){

                     for (var prop in this.kpilist[i].properties) // copy the attributes from the treenode to the new shape
                    {
                        if (this.kpilist[i].hasOwnProperty(prop))
                        {

                                command = new ORYX.Core.Commands["PropertyChange"](prop, event.elements, "", this.kpilist[i][prop], this.facade);
                        }

                    }
                    this.facade.executeCommands([command]);
               }
            }



             return; // ignore double names with kpis
        }
        else*/{
            if (event.name === "oryx-name")
            { // only regard the change of the name not other properties
                var shapes = this.facade.getCanvas().getChildNodes(); //gets all child nodes of canvas
                for (var i = 0; i < shapes.length; i++)
                {
                    if ((shapes[i].properties["oryx-name"] === event.value) && (shapes[i].id !== event.elements[0].id))  // same Shapename already exists
                    { //name exists already & we ignore the shape of the event
                        var command = new ORYX.Core.Commands["PropertyChange"](event.name, event.elements, event.value, "", this.facade);    // set the name back to the old one
                        this.facade.executeCommands([command]);
                    }
                }
            }
        }
    }


});